
function changeRadio(e){
  var that = this;
  that.changeRadio=function(e){
    var index = e.target.dataset.index;
    var dataKey = e.target.dataset.dataKey;
    var value = e.target.dataset.value;
    var obj = {};
    if(index==undefined){
      if (that.data[value] == true) {
        obj[value] = false;
        that.setData(obj);
      } else {
        obj[value] = true;
        that.setData(obj);
      }
    }else{
      var data = that.data[dataKey];
      data.forEach(function(ele){
        ele.checked=false;
      })
      data[index].checked = true;
      obj[dataKey]=data;
      obj[value] = data[index].key || data[index].value;
      that.setData(obj);
    }
  }
}

function changeInput(e){
  var that = this;
  that.changeInput=function(e) {
    this.setData({
      [e.target.dataset.name]:e.detail.value
    })
  }
}

/*获取当前页url*/
function getCurrentPageUrl() {
  var pages = getCurrentPages()    //获取加载的页面
  var currentPage = pages[pages.length - 1]    //获取当前页面的对象
  var url = currentPage.route    //当前页面url
  return url
}

/*获取当前页带参数的url*/
function getCurrentPageUrlWithArgs() {
  var pages = getCurrentPages()    //获取加载的页面
  var currentPage = pages[pages.length - 1]    //获取当前页面的对象
  var url = currentPage.route    //当前页面url
  var options = currentPage.options    //如果要获取url中所带的参数可以查看options

  //拼接url的参数
  var urlWithArgs = url + '?'
  for (var key in options) {
    var value = options[key]
    urlWithArgs += key + '=' + value + '&'
  }
  urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)

  return urlWithArgs
}

module.exports = {
  changeRadio: changeRadio,
  changeInput: changeInput,
  getCurrentPageUrl: getCurrentPageUrl,
  getCurrentPageUrlWithArgs: getCurrentPageUrlWithArgs
}; 